package com.hiyee.user.service.impl;

import com.hiyee.enums.Gender;
import com.hiyee.enums.UserStatus;
import com.hiyee.exception.GraceException;
import com.hiyee.grace.result.ResponseStatusEnum;
import com.hiyee.pojo.AppUser;
import com.hiyee.pojo.bo.UpdateUserInfoBO;
import com.hiyee.user.mapper.AppUserMapper;
import com.hiyee.user.service.UserService;
import com.hiyee.utils.DateUtil;
import com.hiyee.utils.DesensitizationUtil;
import com.hiyee.utils.JsonUtils;
import com.hiyee.utils.RedisOperator;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    AppUserMapper appUserMapper;
    @Autowired
    public Sid sid;
    @Autowired
    RedisOperator redisOperator;

    public static final String DEFAULT_AVATAR = "https://gitee.com/veer6/git-test/raw/master/tupian.jpg";
    // 用户信息
    public static final String REDIS_KEY_USER_INFO = "hiyee:user:info:";

    @Override
    public AppUser getByMobile(String mobile) {
        Example appUserExample = new Example(AppUser.class);
        Example.Criteria criteria = appUserExample.createCriteria();
        criteria.andEqualTo("mobile", mobile);
        AppUser user = appUserMapper.selectOneByExample(appUserExample);
        return user;
    }

    @Transactional
    @Override
    public AppUser createUser(String mobile) {
        AppUser user = new AppUser();
        user.setId(sid.nextShort());
        user.setMobile(mobile);
        user.setNickname(DesensitizationUtil.commonDisplay(mobile));
        user.setBirthday(DateUtil.stringToDate("1900-01-01"));
        user.setSex(Gender.SECRET.getValue());
        user.setFace(DEFAULT_AVATAR);
        user.setActiveStatus(UserStatus.INACTIVE.getValue());
        user.setTotalIncome(0);
        user.setCreatedTime(new Date());
        user.setUpdatedTime(new Date());

        appUserMapper.insert(user);

        return user;
    }

    @Override
    public AppUser getById(String userId) {
        return appUserMapper.selectByPrimaryKey(userId);
    }

    @Override
    public void updateUser(UpdateUserInfoBO updateUserInfoBO) {

        String userId = updateUserInfoBO.getId();

        // 保证双写一致，删除 redis 数据
        redisOperator.del(REDIS_KEY_USER_INFO + userId);

        AppUser appUser = new AppUser();
        BeanUtils.copyProperties(updateUserInfoBO, appUser);

        appUser.setUpdatedTime(new Date());
        appUser.setActiveStatus(UserStatus.ACTIVE.getValue());

        int result = appUserMapper.updateByPrimaryKeySelective(appUser);
        if(result != 1) {
            GraceException.display(ResponseStatusEnum.USER_UPDATE_FAIL);
        }

        // 存入 redis 缓存
        AppUser user = getById(userId);
        redisOperator.set(REDIS_KEY_USER_INFO + userId, JsonUtils.objectToJson(user));

        // 缓存双删策略
        try {
            Thread.sleep(100);
            // 保证双写一致，删除 redis 数据
            redisOperator.del(REDIS_KEY_USER_INFO + userId);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
